package com.hjq.fcloud.ui.activity

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.view.View
import com.hjq.fcloud.R
import com.hjq.fcloud.bean.FlowDataBean
import com.hjq.fcloud.bean.MettingData
import com.hjq.fcloud.event.EventBean
import com.hjq.fcloud.event.EventBusUtils
import com.hjq.fcloud.http.api.MeetingSummaryDetailApi
import com.hjq.fcloud.http.api.SubmitMeetingSummaryApi
import com.hjq.fcloud.http.model.HttpData
import com.hjq.fcloud.ktx.ViewKtx.setVisibility
import com.hjq.fcloud.jump.CommonJumpBean
import com.hjq.fcloud.other.AppFilesHelper
import com.hjq.fcloud.other.SignRefershHelper
import com.hjq.http.EasyHttp
import com.hjq.http.body.JsonBody
import com.hjq.http.listener.HttpCallback
import kotlinx.android.synthetic.main.activity_meeting_summary.*
import okhttp3.Call
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 会议纪要会签
 */
class MeetingSummaryActivity : BaseDetailActivity() {

    companion object {
        //5通过，6不通过
        const val STATUS_PASS = "5"
        const val STATUS_NOT_PASS = "6"

        fun start(ac: Context, jumpBean: CommonJumpBean) {
            var intent = Intent(ac, MeetingSummaryActivity::class.java)
            intent.putExtra(INTENT_IN_ITEM, jumpBean)
            ac.startActivity(intent)
        }
    }

    private var status:String = STATUS_PASS

    override fun userEventBus(): Boolean {
        return true
    }

    override fun getStateLayout(): View? {
       return statusLayout
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_meeting_summary
    }

    override fun initView() {
    }

    override fun initData() {
        getServerDetail()

        getServerFlow()
    }

    override fun setFlowViewData(data: FlowDataBean?) {
//        if (canSignFromList) {
//            flowView.setVisibility(false)
//
//            //审核控件设置
//            feedbackRootView.setVisibility(true)
//            bottomView.setVisibility(true)
//        } else {
//            flowView.setVisibility(true)
//            flowView.setViewData(data?.setup_list)
//        }
    }

    private fun getServerDetail() {
        showAppLoading()
        var api = MeetingSummaryDetailApi()
            .setId(commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(api)
            .request(object : HttpCallback<HttpData<MettingData?>?>(this) {
                override fun onStart(call: Call?) {}
                override fun onSucceed(data: HttpData<MettingData?>?) {
                    setViewData(data?.getData())
                }

                override fun onEnd(call: Call?) {
                    super.onEnd(call)
                    showAppContent()
                }
            })
    }

    private fun setViewData(item: MettingData?) {
        titleSb.setRightText(item?.title)
        projectNameSb.setRightText(item?.project?.project_name)
        sectNameSb.setRightText(item?.sect?.sect_name)
        projectCorpNameSb.setRightText(item?.project?.construct_corp?.corp_name)
        sectBuildCorpName.setRightText(item?.sect?.build_corp?.corp_name)
        resumeView.setTitle("会议纪要概述")
        resumeView.setContent(item?.resume)

        fileView.setViewData(this, "会议纪要", AppFilesHelper.getAllFiles(item?.files))

    }

    fun handlerReject(view: View) {
        status = STATUS_NOT_PASS
        if(checkInput()){
            FaceDetectRGBActivity.launcher(context, intent.extras)
        }
    }

    fun handlerConfirm(view: View) {
        status = STATUS_PASS
        if(checkInput()){
            FaceDetectRGBActivity.launcher(context, intent.extras)
        }
    }

    private fun checkInput(): Boolean {
        var reason = feedbackView.getInPutText()
        if(TextUtils.isEmpty(reason)){
            showToast("请输入意见反馈")
            return false
        }
        return true
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMainEventBus(bean: EventBean) {
        if (bean != null && bean.type == EventBusUtils.TYPE_FACE_AND_SIGN_FINISH) {
            var parms = bean.parms as HashMap<String, String?>
            requestServerConfirm(parms)
        }
    }

    private fun requestServerConfirm(signParms: HashMap<String, String?>) {
        var parms = HashMap<String, String?>()
        if(signParms != null){
            parms.putAll(signParms)
        }
        parms.put("reason",feedbackView.getInPutText())
        parms.put("status",status)
        parms.put("biz_code",commonJumpBean?.bizCode)
        parms.put("biz_id",commonJumpBean?.bizId)
        EasyHttp.post(this)
            .api(SubmitMeetingSummaryApi())
            .body(JsonBody(parms))
            .request(object : HttpCallback<HttpData<MettingData?>?>(this) {
                override fun onSucceed(data: HttpData<MettingData?>?) {
                    SignRefershHelper.signSuccessRefersh(context,commonJumpBean)
                    showToast(data?.getMessage())
                }
            })
    }

}